package com.tl.mybatis03.mapper;

import com.tl.mybatis03.model.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户mapper
 */
public interface UserMapper {
    /***************************** 事务的回滚 ***********************************/
    //插入用户
    int insertUser(User user);

    /***************************** 懒加载测试  ***********************************/

    //根据id查询
    User findById(int id);

    /***************************** 使用注解的方式进行 ***********************************/
    //    @Insert:实现新增
    @Insert("insert into user(username, password, nickname, age, birthday) value (#{username},#{password},#{nickname},#{age},#{birthday})")
    int insert(User user);

    //    @Update:实现更新
    @Update("update user set nickname = #{nickname} where id = #{id}")
    int update(User user);

    //    @Delete:实现删除
    @Delete("delete from user where id = #{id}")
    int del(Integer id);

    //    @Select:实现查询
    @Select("select * from user")
    List<User> select();

    //    @Results:可以与@Result 一起使用，封装多个结果集
    //    @Result:实现结果集封装
    @Select("select * from user where id = #{id}")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "id", property = "addressList", many = @Many(select = "com.tl.mybatis03.mapper.AddressMapper.findUserId"))
    })
    User results(Integer id);


}
